package com.merry.boot.config;

import java.util.List;

import com.merry.boot.converter.MyMessageConverter;
import com.merry.boot.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @author ybzou
 * web的配置类
 * DelegatingWebMvcConfiguration:是spring为我们提供的默认配置类，通过重写这个类中的部分方法，
 *      可以实现向容器中注入自定义的参数处理器，自定义的返回结果处理器等。
 */
@Configuration
public class MyWebConfig extends DelegatingWebMvcConfiguration {
    /**
     *
     * @param argumentResolvers
     */
    @Override
    protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        //argumentResolvers.add(new MyArgumentResolver());
    }

    /**
     *
     * @param returnValueHandlers
     */
    @Override
    protected void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
        //returnValueHandlers.add(new MyReturnValueHandler());
    }

    /**
     * 添加自定义的converter
     * @param converters
     */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MyMessageConverter myMessageConverter = new MyMessageConverter();
        converters.add(myMessageConverter);
    }

    /**
     * 配置拦截器
     * @param registry
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        LoginInterceptor loginInterceptor = new LoginInterceptor();
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")  //所有请求都被拦截包括静态资源
                .excludePathPatterns("/","/login","/css/**","/fonts/**","/images/**","/js/**"); //放行的请求

    }
}
